package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.WOrderOpeara;
import com.ruoyi.system.domain.WOrderOpearaGood;
import com.ruoyi.system.domain.vo.WOrderOpearaGoodVO;
import com.ruoyi.system.domain.vo.WOrderOpearaVO;
import com.ruoyi.system.mapper.WOrderOpearaGoodMapper;
import com.ruoyi.system.mapper.WOrderOpearaMapper;
import com.ruoyi.system.service.IWOrderOpearaGoodService;

/**
 * 物品库存记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-05
 */
@Service
public class WOrderOpearaGoodServiceImpl implements IWOrderOpearaGoodService 
{
    @Autowired
    private WOrderOpearaGoodMapper wOrderOpearaGoodMapper;
    @Autowired
    private WOrderOpearaMapper wOrderOpearaMapper;

    /**
     * 查询物品库存记录
     * 
     * @param id 物品库存记录主键
     * @return 物品库存记录
     */
    @Override
    public WOrderOpearaGood selectWOrderOpearaGoodById(Long id)
    {
        return wOrderOpearaGoodMapper.selectWOrderOpearaGoodById(id);
    }

    /**
     * 查询物品库存记录列表
     * 
     * @param wOrderOpearaGood 物品库存记录
     * @return 物品库存记录
     */
    @Override
    public List<WOrderOpearaGood> selectWOrderOpearaGoodList(WOrderOpearaGood wOrderOpearaGood)
    {
        return wOrderOpearaGoodMapper.selectWOrderOpearaGoodList(wOrderOpearaGood);
    }
    
    @Override
    public List<WOrderOpearaGoodVO> selectWOrderOpearaGoodListNew(WOrderOpearaGoodVO wOrderOpearaGoodVO) {
    	return wOrderOpearaGoodMapper.selectWOrderOpearaGoodListNew(wOrderOpearaGoodVO);
    }
    
    @Override
    public R<WOrderOpearaVO> returnGoodsAll(WOrderOpearaVO wOrderOpearaVO) {
    	Long id = wOrderOpearaVO.getId();
    	if(id == null ) return R.error("没传发料id");
    	String returnExpress = wOrderOpearaVO.getReturnExpress();
    	if(StringUtils.isBlank(returnExpress)) return R.error("没传退回物流公司名字");
    	String returnNo = wOrderOpearaVO.getReturnNo();
    	if(StringUtils.isBlank(returnNo)) return R.error("没传退回单号");
    	WOrderOpeara selectWOrderOpearaById = wOrderOpearaMapper.selectWOrderOpearaById(id);
    	Integer status = selectWOrderOpearaById.getStatus();
    	
    	Integer type = selectWOrderOpearaById.getType();
    	if(type != 1 ) return R.error("不是发料单");
    
    	if(status != 2 ) return R.error("状态不对，不能全部退回");
    	
    	selectWOrderOpearaById.setReturnExpress(returnExpress);
    	selectWOrderOpearaById.setReturnNo(returnNo);
    	selectWOrderOpearaById.setStatus(4);
    	selectWOrderOpearaById.setReturnTime(new Date());
    	selectWOrderOpearaById.setReturnBy(SecurityUtils.getLoginUser().getUsername());
    	wOrderOpearaMapper.updateWOrderOpeara(selectWOrderOpearaById);
    	return R.success();
    }

    /**
     * 新增物品库存记录
     * 
     * @param wOrderOpearaGood 物品库存记录
     * @return 结果
     */
    @Override
    public int insertWOrderOpearaGood(WOrderOpearaGood wOrderOpearaGood)
    {
        wOrderOpearaGood.setCreateTime(DateUtils.getNowDate());
        return wOrderOpearaGoodMapper.insertWOrderOpearaGood(wOrderOpearaGood);
    }

    /**
     * 修改物品库存记录
     * 
     * @param wOrderOpearaGood 物品库存记录
     * @return 结果
     */
    @Override
    public int updateWOrderOpearaGood(WOrderOpearaGood wOrderOpearaGood)
    {
        wOrderOpearaGood.setUpdateTime(DateUtils.getNowDate());
        return wOrderOpearaGoodMapper.updateWOrderOpearaGood(wOrderOpearaGood);
    }

    /**
     * 批量删除物品库存记录
     * 
     * @param ids 需要删除的物品库存记录主键
     * @return 结果
     */
    @Override
    public int deleteWOrderOpearaGoodByIds(Long[] ids)
    {
        return wOrderOpearaGoodMapper.deleteWOrderOpearaGoodByIds(ids);
    }

    /**
     * 删除物品库存记录信息
     * 
     * @param id 物品库存记录主键
     * @return 结果
     */
    @Override
    public int deleteWOrderOpearaGoodById(Long id)
    {
        return wOrderOpearaGoodMapper.deleteWOrderOpearaGoodById(id);
    }
}
